<?php

namespace app\common\controller\zhongzhuanv2;

use app\admin\library\Auth;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Model;
use think\Session;
use fast\Tree;
use think\Validate;
use ec\Ecbase as Eccommon;

/**
 * 后台控制器基类
 */
class Fenpei extends Controller{
    
    
    /**
     * 添加客户 并且更新百应录音 / 意向度内容
     */
   
    public static function addCustomer($request_data) {
        if(empty($request_data['kehu_list'])) return;
        
        $field_id_config = config('zhongzhuan.field');
        $yixiang_field_id = $field_id_config['yixiang']['fieldid'];
        $baiying_luyin_field_id = $field_id_config['baiying_luyin']['fieldid'];
        
        $ecObj = new Eccommon();
                
       $list = $request_data['kehu_list'];
        $optuserid  = $request_data['opt_userid'];
        $org_kehu_list = $request_data['org_kehu_list'];
        // 1. 组装请求数据
        $data = array('list' => $list,'optUserId' =>$optuserid);
        $jsonData = json_encode($data);

        // 2. 请求服务器处理
        $url = "https://open.workec.com/v2/customer/addCustomer";
        $response = $ecObj->doPost($url, $jsonData);
        $assign_log_data = [];
        if($response['code'] == 200){
            $successIdList = $response['data']['successIdList'];
            $failureList = $response['data']['failureList'];
            $save_data = [];
            $insert_kehu_list = [];
            
            $success_mobile = [];
            foreach ($successIdList as $value){
                
                $sucess_org_item = $list[$value['index']];
                $baiying_luyin = $sucess_org_item['fields'][$baiying_luyin_field_id];
                $middle_data = [];
                $middle_data['follow_userid'] = empty($sucess_org_item['followUserId'])? 0 : $sucess_org_item['followUserId']; 
                $middle_data['mobile'] = $sucess_org_item['mobile'];                
                $middle_data['crmId'] = $value['crmId'];
                $middle_data['name'] = $sucess_org_item['name'];
                $middle_data['record_file'] = $baiying_luyin;
                $middle_data['luyin_md5'] = md5($baiying_luyin);
                $middle_data['yixiang_txt'] = $sucess_org_item['fields'][$yixiang_field_id];
                $middle_data['createtime'] = time();
                $save_data[] = $middle_data;
                
                $success_mobile[] = $sucess_org_item['mobile'];
                
                
//                db('ec_aiuser_to_ecuser')->where('mobile',$sucess_org_item['mobile'])->update([
//                    'tuisong_result_txt'=>'成功推送',
//                    'crmId'=>$value['crmId'],
//                    'status_code'=> empty($value['code']) ? 99999 :$value['code'],
//                    'updatetime'=>time(),
//                    'status'=>1,
//                ]);             
            }
            if(!empty($success_mobile)){
                db('ec_aiuser_to_ecuser')->where('mobile','in',$success_mobile)->update([
                    'tuisong_result_txt'=>'成功推送',
                    'updatetime'=>time(),
                    'status'=>1,
                ]);    
            }
            
            
            foreach ($failureList as $value){
                
                db('ec_aiuser_to_ecuser')->where('id',$org_kehu_list[$value['index']]['id'])->update([
                    'tuisong_result_txt'=>$value['failureCause'],
                    'update_kehu_info_status'=>1,
                    'status_code'=> empty($value['code']) ? 99999 :$value['code'],
                    'updatetime'=>time(),
                    'status'=>2,
                ]);  
            }
            
            if(!empty($save_data)){
                db('ec_kehu')->insertAll($save_data);
            }
            
            
        } 
        qqq('addCustomer',$response);
        return $response;

    }
    
    
    /**
     * 更新客户标签  意向度 和客户来源 修改客户标签（支持批量）
     */
    public static function updateKehuLabel($request_data){
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/label/update';
        $data = ['optUserId' => $request_data['optUserId'],'crmIds' => $request_data['crmIds'],'labels' => $request_data['labels'],'type' => 1];        
        $result = $ecObj->doPost($request_url, json_encode($data));        
        return $result;
    }
    
     /**
     * 更新百应录音
     */
    public static function updateInfo($request_data){
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/customer/batchUpdateCustomer';
        $data  = $request_data['list'];     
        $result = $ecObj->doPost($request_url, json_encode($data));

        return $result;
    }
    
    
    public static function getKehuInfoByOptUserid($request_data) {
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/customer/queryList';
        $result = $ecObj->doPost($request_url, json_encode($request_data));

        return $result;
    }
    
    public static function getKehuInfoByMobile($request_data) {
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/customer/queryExist';
        $data = ['mobile' => $request_data['mobile']];
        $result = $ecObj->doPost($request_url, json_encode($data));

        return $result;
    }
    
    public static function getKehuInfoByCrmId($request_data) {
        $ecObj = new Eccommon();
         $request_url = 'https://open.workec.com/v2/customer/queryList';
        $data = ['crmIds' => $request_data['crmIds']];
        $result = $ecObj->doPost($request_url, json_encode($data));

        return $result;
    }
    
    public static function updateKehuOwnwer($request_data){
        
         $ecObj = new Eccommon();
         $request_url = 'https://open.workec.com/v2/customer/change/user';;
        $result = $ecObj->doPost($request_url, json_encode($request_data));

        return $result;
        
    }
    
//    放弃客户(支持批量)
    public static function abandonKehu($request_data) {
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/customer/change/abandon';
        $result = $ecObj->doPost($request_url, json_encode($request_data));

        return $result;
    }
    
    
    //   联系记录 - 批量添加跟进记录
    public static function saveUserTrajectory($request_data) {
        $ecObj = new Eccommon();
        $request_url = 'https://open.workec.com/v2/trajectory/saveUserTrajectory';
        $result = $ecObj->doPost($request_url, json_encode($request_data));

        return $result;
    }

}
